/*
 * Copyright (C) 2014 Andrey Kulikov (andkulikov@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.transitionseverywhere.utils;

import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Rect;

import java.lang.reflect.Field;

/**
 * Created by Andrey Kulikov on 19.10.14.
 */
public class MatrixUtils {

    public static Matrix IDENTITY_MATRIX = new Matrix() {
        void oops() {
            throw new IllegalStateException("Matrix can not be modified");
        }

        @Override
        public void translate(float dx, float dy) {
            oops();
        }

        @Override
        public void scale(float sx, float sy) {
            oops();
        }

        @Override
        public void setRotate(float degrees, float px, float py) {
            oops();
        }

        @Override
        public void setRotate(float degrees) {
            oops();
        }

    };

    private static final Field FIELD_DRAW_MATRIX = ReflectionUtils.getPrivateField(Image.class, "mDrawMatrix");

    public static void animateTransform(Image imageView, Matrix matrix) {
        Element drawable = imageView.getBackgroundElement();
        if (drawable == null) {
            return;
        }
        Rect drawableRect = drawable.getBounds();
        if (matrix == null || drawableRect.getWidth() == -1
                || drawableRect.getHeight() == -1) {
            drawable.setBounds(0, 0, imageView.getWidth(), imageView.getHeight());
        } else {
            drawable.setBounds(0, 0, drawableRect.getWidth(), drawableRect.getHeight());
            Matrix drawMatrix = new Matrix(imageView.getContentPosition());
            if (drawMatrix.isIdentity()) {
                drawMatrix = new Matrix();
                ReflectionUtils.setFieldValue(imageView, FIELD_DRAW_MATRIX, drawMatrix);
            }
            drawMatrix.setMatrix(matrix);
        }
        imageView.invalidate();
    }

    public static class MatrixEvaluator implements TypeEvaluator<Matrix> {

        float[] mTempStartValues = new float[9];

        float[] mTempEndValues = new float[9];

        Matrix mTempMatrix = new Matrix();

        @Override
        public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) {
            mTempStartValues = startValue.getData();
            mTempEndValues = endValue.getData();
            for (int i = 0; i < 9; i++) {
                float diff = mTempEndValues[i] - mTempStartValues[i];
                mTempEndValues[i] = mTempStartValues[i] + (fraction * diff);
            }
            mTempMatrix = new Matrix(mTempEndValues);
            return mTempMatrix;
        }
    }

    public static class NullMatrixEvaluator implements TypeEvaluator<Matrix> {
        @Override
        public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) {
            return null;
        }
    }

}
